package pl.edu.agh.pros.connector.gae;

import pl.edu.agh.pros.conf.XMPPConfiguration;
import pl.edu.agh.pros.connector.IServerConnector;

import com.google.gson.Gson;

@SuppressWarnings("nls")
public class GAEServerConnector implements IServerConnector {

	private final Gson gson = new Gson();

	private final String url;

	public GAEServerConnector(String url) {
		this.url = url + "/rest/gae";
	}

	@Override
	public XMPPConfiguration getXMPPConfiguration(String appKey, String devId) {
		RestClient client = new RestClient(url + "/xmpp");
		client.addParam("appKey", appKey);
		client.addParam("devId", devId);

		try {
			client.execute(RequestMethod.GET);
		} catch (Exception e) {
			e.printStackTrace();
		}

		String response = client.getResponse();
		XMPPConfiguration xmppConfiguration = gson.fromJson(response, XMPPConfiguration.class);

		return xmppConfiguration;
	}

	@Override
	public Long register(String appKey, String devId, String xmppAddress) {
		RestClient client = new RestClient(url + "/regcli");
		client.addParam("appKey", appKey);
		client.addParam("devId", devId);
		client.addParam("xmppAddress", xmppAddress);

		try {
			client.execute(RequestMethod.GET);
		} catch (Exception e) {
			e.printStackTrace();
		}

		String response = client.getResponse();
		if (response == null) {
			return 1L;
		}
		String result_s = response.substring(0, response.length() - 1);
		Long result = 1L;
		try {
			result = Long.valueOf(result_s);
		} catch (NumberFormatException e) {
			// ignore
		}

		return result;
	}

	@Override
	public String getXmppAddress(String appKey, String devId) {
		RestClient client = new RestClient(url + "/addr");
		client.addParam("appKey", appKey);
		if (devId != null) {
			client.addParam("devId", devId);
		}

		try {
			client.execute(RequestMethod.GET);
		} catch (Exception e) {
			e.printStackTrace();
		}

		String response = client.getResponse();
		String result = response.substring(0, response.length() - 1);

		return result;
	}

	@Override
	public Long registerTopic(String appKey, String topicName, String roomName) {
		RestClient client = new RestClient(url + "/regto");
		client.addParam("appKey", appKey);
		client.addParam("topicName", topicName);
		client.addParam("roomName", roomName);

		try {
			client.execute(RequestMethod.GET);
		} catch (Exception e) {
			e.printStackTrace();
		}

		String response = client.getResponse();
		String result_s = response.substring(0, response.length() - 1);
		Long result = Long.valueOf(result_s);

		return result;
	}

	@Override
	public void unregisterTopic(String appKey, String topicName) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getRoomName(String appKey, String topicName) {
		RestClient client = new RestClient(url + "/room");
		client.addParam("appKey", appKey);
		client.addParam("topicName", topicName);

		try {
			client.execute(RequestMethod.GET);
		} catch (Exception e) {
			e.printStackTrace();
		}

		String response = client.getResponse();
		String result = response.substring(0, response.length() - 1);

		return result;
	}

}
